package com.caroil.caroilservice.activitys.login.fragment;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.caroil.caroilservice.R;
import com.caroil.caroilservice.activitys.login.vmode.LoginMode;
import com.caroil.caroilservice.base.BaseApplation;
import com.caroil.caroilservice.base.BaseFragment;
import com.caroil.caroilservice.view.dialog.LoadingDialog;
import butterknife.BindView;
import butterknife.OnClick;


/**
 * 登录主页
 */
public class LoginFragment extends BaseFragment {
    public static LoginFragment newInstance() {
        LoginFragment loginFragment = new LoginFragment();
        return loginFragment;
    }

    @BindView(R.id.btn_login)
    Button btn_login;
    @BindView(R.id.checkbox)
    ImageView checkBox;
    @BindView(R.id.progressbar)
    LoadingDialog mProgressbar;
    private LoginMode loginMode;
    private int winWidth;
    private int winHeight;
    private boolean isSelect = true;

    @Override
    protected int initLayout() {
        return R.layout.ac_login;
    }

    @Override
    protected void initView(View view) {
        setBarState(true);
        Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        if (point.x > point.y) {
            winHeight = point.x;
            winWidth = point.y;
        } else {
            winHeight = point.y;
            winWidth = point.x;
        }
    }

    @Override
    protected void initData(Context mContext) {
        loginMode = getViewModelProvider().get(LoginMode.class);
    }

    @Override
    protected void loadMethod() {
        colseDialog();
        getPhone();
    }

    @OnClick({R.id.btn_login, R.id.tv_ysxy, R.id.ly_checkbox, R.id.checkbox, R.id.ly_other})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login://本机号码一键登录
                mProgressbar.show();
                loginMode.judgeIfHavePhonePermission(getActivity(), winWidth, winHeight);
                break;
            case R.id.tv_ysxy://隐私协议
                start(AgreementWebFragment.newInstance());
                break;
            case R.id.ly_checkbox://隐私协议选中开关
                xySelect();
                break;
            case R.id.checkbox://隐私协议选中开关
                xySelect();
                break;
            case R.id.ly_other://手机号验证码登录
                start(PhoneCodeLoginFragment.newInstance());
                break;

        }
    }

    private void xySelect() {
        if (isSelect) {
            isSelect = false;
            checkBox.setImageResource(R.mipmap.icon_login_check);
            btn_login.setBackgroundResource(R.mipmap.login_btn_no);
            btn_login.setTextColor(Color.parseColor("#999999"));
            btn_login.setClickable(false);
        } else {
            isSelect = true;
            checkBox.setImageResource(R.mipmap.icon_login_checkpre);
            btn_login.setBackgroundResource(R.mipmap.login_btn);
            btn_login.setTextColor(Color.parseColor("#ffffff"));
            btn_login.setClickable(true);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setBarState(true);
        }
    }

    /**
     * 获取本机号码
     */
    private void getPhone() {
        loginMode.getLivePhone().observe(this, code -> {
            mProgressbar.dismiss();
            if (code.equals("200")) {
                Toast.makeText(BaseApplation.instance(), "登录成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 如果开启了授权界面则关闭当前的loading
     */
    private void colseDialog() {
        getActivity().getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (activity.getLocalClassName().equals("com.cmic.sso.sdk.activity.LoginAuthActivity") || activity.getLocalClassName().equals("cn.jiguang.verifysdk.CtLoginActivity")) {
                    mProgressbar.dismiss();
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

}
